package org.csu.webjpetstoreback.service.serviceImpl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.csu.webjpetstoreback.config.CommonResponse;
import org.csu.webjpetstoreback.mapper.ProfileMapper;
import org.csu.webjpetstoreback.mapper.SignonMapper;
import org.csu.webjpetstoreback.model.Account;
import org.csu.webjpetstoreback.model.Profile;
import org.csu.webjpetstoreback.model.Signon;
import org.csu.webjpetstoreback.service.ProfileService;
import org.csu.webjpetstoreback.service.SignonService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service

public class ProfileServiceImpl extends ServiceImpl<ProfileMapper, Profile> implements ProfileService {

    @Autowired
    private ProfileMapper profileMapper;

    @Override
    public CommonResponse<Profile> GetAccountByID(String userid) {
        QueryWrapper<Profile> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("userid",userid);
        Profile profile = profileMapper.selectOne(queryWrapper);
        if (profile != null){
            return CommonResponse.BackForSuccess(profile);
        }else {
            return CommonResponse.BackForError("该用户不存在profile选项");
        }
    }

    @Override
    public CommonResponse<Profile> PostAccount(Profile profile) {
        int result = profileMapper.insert(profile);
        if (result ==1){
            return CommonResponse.BackForSuccess(profile);
        }else{
            return CommonResponse.BackForError("新增失败");
        }
    }

    @Override
    public CommonResponse<Profile> PutAccount(Profile profile) {
        UpdateWrapper<Profile> UpdateWrapper = new UpdateWrapper<>();
        int result = profileMapper.update(profile,UpdateWrapper);
        if (result ==4){
            return CommonResponse.BackForSuccess(profile);
        }else{
            return CommonResponse.BackForError("profile修改失败");
        }
    }

    @Override
    public CommonResponse<Profile> DeleteAccount(String userid) {
        int result = profileMapper.deleteById(userid);
        if (result ==1){
            return CommonResponse.BackForSuccess("删除成功!!!");
        }else{
            return CommonResponse.BackForError("删除失败");
        }
    }
}
